package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

const PATTERN = "2006-01-02 15:04:05"

func main() {
	engine := gin.Default()
	engine.LoadHTMLGlob("template/*")
	engine.GET("/hello", func(ctx *gin.Context) {
		ctx.JSON(http.StatusOK, gin.H{
			"message": "world",
		})
	})
	engine.GET("/index", func(c *gin.Context) {
		c.HTML(http.StatusOK, "index.tmpl", gin.H{
			"title": "hello gin",
			"date": time.Now().Format(PATTERN),
		})
	})
	engine.GET("/getStudent", getStudent)
	engine.Run()
}

type StudentReq struct {
	StuNo string	`form:"stuNo" binding:"required"`
}
// 获取学生信息
func getStudent(c *gin.Context) {
	var req StudentReq
	err := c.ShouldBind(&req)
	if err == nil {
		if req.StuNo == "root" {
			c.JSON(http.StatusOK, gin.H{"stuNo": req.StuNo, "date": time.Now().Format("2006-01-02 15:04:05")})
		} else {
			c.JSON(http.StatusOK, gin.H{"stuNo": req.StuNo})
		}
	}
}
